package net.lab1024.sa.admin.module.users.approval.service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import net.lab1024.sa.admin.module.users.approval.dao.ConsumablesPurchaseApprovalDao;
import net.lab1024.sa.admin.module.users.approval.domain.entity.ConsumablesPurchaseApprovalEntity;
import net.lab1024.sa.admin.module.users.approval.domain.form.ConsumablePurchaseApprovalFrom;
import net.lab1024.sa.admin.module.users.approval.domain.form.ConsumablesPurchaseApprovalAddForm;
import net.lab1024.sa.admin.module.users.approval.domain.form.ConsumablesPurchaseApprovalQueryForm;
import net.lab1024.sa.admin.module.users.approval.domain.form.ConsumablesPurchaseApprovalUpdateForm;
import net.lab1024.sa.admin.module.users.approval.domain.vo.ConsumablesPurchaseApprovalVO;
import net.lab1024.sa.admin.module.users.consumablesinventory.dao.ConsumablesInventoryDao;
import net.lab1024.sa.admin.module.users.consumablesinventory.domain.entity.ConsumablesInventoryEntity;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 实训室耗材审批管理 Service
 *
 * @Author 王俊权
 * @Date 2024-11-06 10:32:23
 * @Copyright 🐂🐎bloc
 */

@Service
public class ConsumablesPurchaseApprovalService {
    private static final Integer agree = 1;
    private static final Integer disagree = 2;
    private static final Integer unApproval = 0;

    @Resource
    private ConsumablesPurchaseApprovalDao consumablesPurchaseApprovalDao;
    @Resource
    private ConsumablesInventoryDao consumablesInventoryDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<ConsumablesPurchaseApprovalVO> queryPage(ConsumablesPurchaseApprovalQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ConsumablesPurchaseApprovalVO> list = consumablesPurchaseApprovalDao.queryPage(page, queryForm);
        PageResult<ConsumablesPurchaseApprovalVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(ConsumablesPurchaseApprovalAddForm addForm) {
        ConsumablesPurchaseApprovalEntity consumablesPurchaseApprovalEntity = SmartBeanUtil.copy(addForm, ConsumablesPurchaseApprovalEntity.class);
        consumablesPurchaseApprovalDao.insert(consumablesPurchaseApprovalEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(ConsumablesPurchaseApprovalUpdateForm updateForm) {
        ConsumablesPurchaseApprovalEntity consumablesPurchaseApprovalEntity = SmartBeanUtil.copy(updateForm, ConsumablesPurchaseApprovalEntity.class);
        consumablesPurchaseApprovalDao.updateById(consumablesPurchaseApprovalEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        consumablesPurchaseApprovalDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer id) {
        if (null == id){
            return ResponseDTO.ok();
        }

        consumablesPurchaseApprovalDao.deleteById(id);
        return ResponseDTO.ok();
    }

    /**
     * 处理耗材采购审批表单
     *
     * @param approvalForm 耗材采购审批表单
     * @return 响应结果，包含处理状态
     */
    public ResponseDTO<String> approvalPurchase(ConsumablePurchaseApprovalFrom approvalForm) {
        // 将审批表单对象复制到审批实体对象中
        ConsumablesPurchaseApprovalEntity approvalEntity = SmartBeanUtil.copy(approvalForm, ConsumablesPurchaseApprovalEntity.class);
        // 创建一个新的耗材采购审批实体对象
        ConsumablesPurchaseApprovalEntity consumablesPurchaseApprovalEntity = new ConsumablesPurchaseApprovalEntity();
        // 设置新实体对象的ID为表单实体对象的ID
        consumablesPurchaseApprovalEntity.setId(approvalEntity.getId());
        // 设置新实体对象的审批状态为表单实体对象的审批状态
        consumablesPurchaseApprovalEntity.setApprovalStatus(approvalEntity.getApprovalStatus());
        if(approvalEntity.getApprovalStatus() != unApproval){
            Date date = new Date();
            String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
            Timestamp timestamp = Timestamp.valueOf(nowTime);
            consumablesPurchaseApprovalEntity.setApprovalDate(timestamp);
        }
        consumablesPurchaseApprovalEntity.setHeadOpinion(approvalEntity.getHeadOpinion());
        if(approvalEntity.getHeadOpinion() != null){
            Date date = new Date();
            String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
            Timestamp timestamp = Timestamp.valueOf(nowTime);
            consumablesPurchaseApprovalEntity.setHeadOpDate(timestamp);
        }
        consumablesPurchaseApprovalEntity.setDeputyHeadOpinion(approvalEntity.getDeputyHeadOpinion());
        if(approvalEntity.getDeputyHeadOpinion()!= null){
            Date date = new Date();
            String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
            Timestamp timestamp = Timestamp.valueOf(nowTime);
            consumablesPurchaseApprovalEntity.setDeputyHeadOpDate(timestamp);
        }
        Number budgetAmount = approvalEntity.getBudgetAmount();
        if (budgetAmount != null && budgetAmount.doubleValue() > 2000) {
            consumablesPurchaseApprovalEntity.setFunctionDepartmentOpinion(approvalEntity.getFunctionDepartmentOpinion());
            if (approvalEntity.getFunctionDepartmentOpinion() != null) {
                Date date = new Date();
                String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
                Timestamp timestamp = Timestamp.valueOf(nowTime);
                consumablesPurchaseApprovalEntity.setFunctionDepartmentOpDate(timestamp);
            }
            consumablesPurchaseApprovalEntity.setTeachingLeaderOpinion(approvalEntity.getTeachingLeaderOpinion());
            if (approvalEntity.getTeachingLeaderOpinion() != null) {
                Date date = new Date();
                String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
                Timestamp timestamp = Timestamp.valueOf(nowTime);
                consumablesPurchaseApprovalEntity.setTeachingLeaderOpDate(timestamp);
            }
        }
        // 根据ID更新耗材采购审批实体的审批状态
        consumablesPurchaseApprovalDao.updateById(consumablesPurchaseApprovalEntity);
        if (approvalEntity.getApprovalStatus() == agree) {
            // 如果审批状态为同意，执行同意操作
            // 在这里编写同意操作的逻辑
            ConsumablesInventoryEntity InventoryEntity = new ConsumablesInventoryEntity();
            InventoryEntity.setMaterialName(approvalEntity.getMaterialName());
            InventoryEntity.setSpecificationModel(approvalEntity.getSpecification());
            InventoryEntity.setPurchaseQuantity(approvalEntity.getQuantity());
            InventoryEntity.setPurchaser(approvalEntity.getApplicant());
            InventoryEntity.setUnitPrice(approvalEntity.getUnitPrice());
            InventoryEntity.setTotalPrice(approvalEntity.getTotalAmount());
            InventoryEntity.setStorageLocation(approvalEntity.getStorageLocation());
            InventoryEntity.setPurchaseChannel(approvalEntity.getPurchaseChannel());
            InventoryEntity.setUnit(approvalEntity.getUnit());
            InventoryEntity.setCategory(approvalEntity.getCategory());
            InventoryEntity.setMaterialCode(approvalEntity.getMaterialCode());
            consumablesInventoryDao.insert(InventoryEntity);
        }
        return ResponseDTO.ok();
    }

    /**
     * 批量插入
     * @param consumablesPurchaseApprovalEntityList
     * @return
     * @author lyy
     */

    public ResponseDTO<String> batchInsert(List<ConsumablesPurchaseApprovalEntity> consumablesPurchaseApprovalEntityList) {
        if (CollectionUtils.isEmpty(consumablesPurchaseApprovalEntityList)){
            return ResponseDTO.ok();
        }

        consumablesPurchaseApprovalEntityList.forEach(consumablesPurchaseApprovalEntity -> {

        });
        consumablesPurchaseApprovalDao.insertBatch(consumablesPurchaseApprovalEntityList);
        return ResponseDTO.ok();
    }


}
